﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace UBCon.Controls
{
    public static class XMessageBox
    {
        /// <summary>
        /// Displays a message box with specified message and returns a result.
        /// </summary>
        /// <param name="message">Message to display.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string message)
        {
            return ShowWindow(string.Empty, message, MessageBoxButton.OK, MessageBoxImage.Information, null, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified message and returns a result.
        /// </summary>
        /// <param name="message">Text to display</param>
        /// <param name="owner">A window that represents the owner window of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string message, Window owner)
        {
            return ShowWindow(string.Empty, message, MessageBoxButton.OK, MessageBoxImage.Information, owner, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified message, image and returns a result.
        /// </summary>
        /// <param name="message">Text to display.</param>
        /// <param name="image">Image to display.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string message, MessageBoxImage image)
        {
            return ShowWindow(string.Empty, message, MessageBoxButton.OK, image, null, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified message, image, owner and returns a result.
        /// </summary>
        /// <param name="message">Text to display.</param>
        /// <param name="image">Image to display.</param>
        /// <param name="owner">A window that represents the owner window of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string message, MessageBoxImage image, Window owner)
        {
            return ShowWindow(string.Empty, message, MessageBoxButton.OK, image, owner, MessageBoxResult.OK);
        }


        /// <summary>
        /// Displays a message box with specified message, button and returns a result.
        /// </summary>
        /// <param name="message">Text to display.</param>
        /// <param name="button">A MessageBoxImage value that specifies the icon to display.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string message, MessageBoxButton button)
        {
            return ShowWindow(string.Empty, message, button, MessageBoxImage.Information, null, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified message, button, default return result.
        /// </summary>
        /// <param name="message">Text to display.</param>
        /// <param name="button">A MessageBoxImage value that specifies the icon to display.</param>
        /// <param name="defaultResult">A value that specifies the default result of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string message, MessageBoxButton button, MessageBoxResult defaultResult)
        {
            return ShowWindow(string.Empty, message, button, MessageBoxImage.Information, null, defaultResult);
        }

        /// <summary>
        /// Displays a message box with specified message, button, owner in front of which message box is displayed.
        /// </summary>
        /// <param name="message">Text to display.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <param name="owner">A window that represents the owner window of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string message, MessageBoxButton button, Window owner)
        {
            return ShowWindow(string.Empty, message, button, MessageBoxImage.Information, owner, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified message, button, owner in front of which message box is displayed and 
        /// default return result.
        /// </summary>
        /// <param name="message">A text that is displayed.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <param name="owner">A window that represents the owner window of the message box.</param>
        /// <param name="defaultResult">A value that specifies the default result of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string message, MessageBoxButton button, Window owner, MessageBoxResult defaultResult)
        {
            return ShowWindow(string.Empty, message, button, MessageBoxImage.Information, owner, defaultResult);
        }

        /// <summary>
        /// Displays a message box with specified message, button and image.
        /// </summary>
        /// <param name="message">A text that is displayed.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <param name="image">A value that specifies the image to display.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string message, MessageBoxButton button, MessageBoxImage image)
        {
            return ShowWindow(string.Empty, message, button, image, null, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified message, button, image and default return result..
        /// </summary>
        /// <param name="message">A text that is displayed.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <param name="image">A value that specifies the image to display.</param>
        /// <param name="defaultResult">A value that specifies the default result of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string message, MessageBoxButton button, MessageBoxImage image, MessageBoxResult defaultResult)
        {
            return ShowWindow(string.Empty, message, button, image, null, defaultResult);
        }

        /// <summary>
        /// Displays a message box with specified message, button, image and owner window.
        /// </summary>
        /// <param name="message">A text that is displayed.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <param name="image">A value that specifies the image to display.</param>
        /// <param name="owner">A window that represents the owner window of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string message, MessageBoxButton button, MessageBoxImage image, Window owner)
        {
            return ShowWindow(string.Empty, message, button, image, owner, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified message, button, image, owner and default return result.
        /// </summary>
        /// <param name="message">A text that is displayed.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <param name="image">A value that specifies the image to display.</param>
        /// <param name="owner">A window that represents the owner window of the message box.</param>
        /// <param name="defaultResult">A value that specifies the default result of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string message, MessageBoxButton button, MessageBoxImage image, Window owner, MessageBoxResult defaultResult)
        {
            return ShowWindow(string.Empty, message, button, image, owner, defaultResult);
        }

        /// <summary>
        /// Displays a message box with specified message and title bar caption.
        /// </summary>
        /// <param name="caption">Text that is displayed in itle bar.</param>
        /// <param name="message">A message that is displayed.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string caption, string message)
        {
            return ShowWindow(caption, message, MessageBoxButton.OK, MessageBoxImage.Information, null, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified title bar caption, message and owner.
        /// </summary>
        /// <param name="caption">Text that is displayed in itle bar.</param>
        /// <param name="message">A message that is displayed.</param>
        /// <param name="owner">A window that represents the owner window of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string caption, string message, Window owner)
        {
            return ShowWindow(caption, message, MessageBoxButton.OK, MessageBoxImage.Information, owner, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified title bar caption, message and owner.
        /// </summary>
        /// <param name="caption">Text that is displayed in itle bar.</param>
        /// <param name="message">A message that is displayed.</param>
        /// <param name="image">A value that specifies the image to display.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string caption, string message, MessageBoxImage image)
        {
            return ShowWindow(caption, message, MessageBoxButton.OK, image, null, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified title bar caption, message, image and owner.
        /// </summary>
        /// <param name="caption">A text that is displayed in itle bar.</param>
        /// <param name="message">A message that is displayed.</param>
        /// <param name="image">A value that specifies the image to display.</param>
        /// <param name="owner">A window that represents the owner window of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string caption, string message, MessageBoxImage image, Window owner)
        {
            return ShowWindow(caption, message, MessageBoxButton.OK, image, owner, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified title bar caption, message, button.
        /// </summary>
        /// <param name="caption">A text that is displayed in title bar.</param>
        /// <param name="message">A message that is displayed.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string caption, string message, MessageBoxButton button)
        {
            return ShowWindow(caption, message, button, MessageBoxImage.Information, null, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified title bar caption, message, button and owner.
        /// </summary>
        /// <param name="caption">A text that is displayed in title bar.</param>
        /// <param name="message">A message that is displayed.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <param name="owner">A window that represents the owner window of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string caption, string message, MessageBoxButton button, Window owner)
        {
            return ShowWindow(caption, message, button, MessageBoxImage.Information, owner, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified title bar caption, message, button and owner.
        /// </summary>
        /// <param name="caption">A text that is displayed in title bar.</param>
        /// <param name="message">A message that is displayed.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <param name="owner">A window that represents the owner window of the message box.</param>
        /// <param name="defaultResult">A value that specifies the default result of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string caption, string message, MessageBoxButton button, Window owner, MessageBoxResult defaultResult)
        {
            return ShowWindow(caption, message, button, MessageBoxImage.Information, owner, defaultResult);
        }

        /// <summary>
        /// Displays a message box with specified title bar caption, message, button and image.
        /// </summary>
        /// <param name="caption">A text that is displayed in title bar.</param>
        /// <param name="message">A message that is displayed.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <param name="image">A value that specifies the image to display.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string caption, string message, MessageBoxButton button, MessageBoxImage image)
        {
            return ShowWindow(caption, message, button, image, null, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified title bar caption, message, button and image.
        /// </summary>
        /// <param name="caption">A text that is displayed in title bar.</param>
        /// <param name="message">A message that is displayed.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <param name="defaultResult">A value that specifies the default result of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string caption, string message, MessageBoxButton button, MessageBoxResult defaultResult)
        {
            return ShowWindow(caption, message, button, MessageBoxImage.Information, null, defaultResult);
        }

        /// <summary>
        /// Displays a message box with specified title bar caption, message, button and image.
        /// </summary>
        /// <param name="caption">A text that is displayed in title bar.</param>
        /// <param name="message">A message that is displayed.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <param name="image">A value that specifies the image to display.</param>
        /// <param name="owner">A window that represents the owner window of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string caption, string message, MessageBoxButton button, MessageBoxImage image, Window owner)
        {
            return ShowWindow(caption, message, button, image, owner, MessageBoxResult.OK);
        }

        /// <summary>
        /// Displays a message box with specified title bar caption, message, button and image.
        /// </summary>
        /// <param name="caption">A text that is displayed in title bar.</param>
        /// <param name="message">A message that is displayed.</param>
        /// <param name="button">A value that specifies which button or buttons to display.</param>
        /// <param name="image">A value that specifies the image to display.</param>
        /// <param name="owner">A window that represents the owner window of the message box.</param>
        /// <param name="defaultResult">A value that specifies the default result of the message box.</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string caption, string message, MessageBoxButton button, MessageBoxImage image, Window owner, MessageBoxResult defaultResult)
        {
            return ShowWindow(caption, message, button, image, owner, defaultResult);
        }

        /// <summary>
        /// Main method which initializes and shows window. 
        /// All overloaded methods (Show) call refer to this method.
        /// </summary>
        /// <param name="caption">A text that is displayed in title bar.</param>
        /// <param name="message">A message that is displayed.</param>
        /// <param name="owner">A window that represents the owner window of the message box</param>
        /// <param name="mButton">A value that specifies which button or buttons to display.</param>
        /// <param name="mImage">A value that specifies the image to display.</param>
        /// <param name="defaultResult">A value that specifies the default result of the message box.</param>
        /// <returns></returns>
        private static MessageBoxResult ShowWindow(string caption, string message,
            MessageBoxButton mButton, MessageBoxImage mImage, Window owner, MessageBoxResult defaultResult)
        {
            if (owner == null) 
            {
                foreach (Window window in Application.Current.Windows)
                {
                    if (window.IsActive) 
                    {
                        owner = window;
                        break;
                    }
                }
            }

            MessageBoxControl mbControl = new MessageBoxControl()
            {
                Title = caption,
                MessageBoxButton = mButton,
                MessageBoxImage = mImage,
                Content = message,
                Owner=owner,
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
                DefaultResult= defaultResult

            };

           mbControl.ShowDialog();
           
           
           MessageBoxResult _result = mbControl.MBDialogResult;

           return mbControl.MBDialogResult;
        }
       
    }
}
